

#include "Losu_Std.h"
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
using namespace std;
extern "C"{
vector<int> pool;
vector<fstream> ptrpool;
extern string rtstr; 
extern LNI_return rt;
LNI_return Els_File_freenum(LNI_argv a){
    rt.type=ELS_TYPE_NUMBER;
    int i;
    for(i=0;i<pool.size();i++){
        rt.num = i;
        if(!pool[i]){ 
            pool[i]=1;    
            return rt;
        }
    }
    pool.push_back(1);
    ptrpool.push_back(fstream());
    rt.num = pool.size()-1;
    return rt;
}
LNI_return Els_File_open(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    switch ((int)a.num[5]){
        case 1: ptrpool[a.num[3]].open(a.str[4],ios::in);
            break;
        case 2: ptrpool[a.num[3]].open(a.str[4],ios::out);
            break;
        case 3: ptrpool[a.num[3]].open(a.str[4],ios::app);
            break;
    }
    return rt; 
}
LNI_return Els_File_print(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    switch(a.type[4]){
        case ELS_TYPE_NUMBER:
            ptrpool[a.num[3]]<<a.num[4];
            break;
        case ELS_TYPE_STRING:
            ptrpool[a.num[3]]<<a.str[4];
            break;
    }
    
    return rt;
}
LNI_return Els_File_input(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    ptrpool[a.num[3]]>>rtstr;
    rt.str = (char*)rtstr.c_str();
    return rt;
}
LNI_return Els_File_lineinput(LNI_argv a){
    rt.type = ELS_TYPE_STRING;
    getline(ptrpool[a.num[3]],rtstr);
    rt.str = (char*)rtstr.c_str();
    return rt;
}
LNI_return Els_File_close(LNI_argv a){
    rt.type = ELS_TYPE_NULL;
    pool[a.num[3]]=0;
    ptrpool[a.num[3]].close();
    return rt;
}
LNI_return Els_File_eof(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    rt.num =  ptrpool[a.num[3]].eof();
    return rt;
}
LNI_return Els_File_isopen(LNI_argv a){
    rt.type = ELS_TYPE_NUMBER;
    rt.num =  ptrpool[a.num[3]].is_open();
    return rt;
}
}